﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Configuration;
using System.Data;
using System.IO;

namespace CollectLib
{
    public class Manage
    {
        public Manage()
        {
        }
        public Manage(string strconnection)
        {

        }
        private MysqlDB _db;
        public MysqlDB db
        {
            get { return _db ?? (_db = new MysqlDB(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString)); }
        }
        #region mysql日期转换

        public DateTime GetDateTimeBy10int(Int64 mysqltime)
        {
            Int64 begtime = mysqltime * 10000000;//100毫微秒为单位,textBox1.text需要转化的int日期
            DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0);
            long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
            long time_tricks = tricks_1970 + begtime;//日志日期刻度
            DateTime dt = new DateTime(time_tricks);//转化为DateTime
            DateTime enddt = dt.Date;//获取到日期整数
            return enddt;
        }

        #endregion
        #region CreatHtml xml
        /// <summary>
        /// 生成单页面静态
        /// </summary>
        public void CreatOneHtml(int id,string wangzhi)
        {
            try
            {
                var url = string.Format("{0}index.php?r=generate&type=video&id={1}", wangzhi, id);
                GetPage(url, Encoding.UTF8);
                Err.writeInLog(string.Format("id:{0},生成html完成", id));
            }
            catch (Exception ex)
            {
                Err.writeInLog(string.Format("id:{0},生成html异常{1}", id, ex.Message));
            }

        }
        /// <summary>
        /// 生成xml
        /// </summary>
        public void CreatXml(string wangzhi)
        {
            try
            {
                var url = string.Format("{0}index.php?r=generate",wangzhi);
                GetPage(url, Encoding.UTF8);
                Err.writeInLog("生成xml完成");
            }
            catch (Exception ex)
            {
                Err.writeInLog("xml:" + ex.Message);
            }

        }
        #endregion
        #region 公共方法
        public DataTable GetDTBySql(string sql)
        {
            return db.GetdataTableBySql(sql);
        }
        public int ExecuteSql(string sql)
        {
            return db.ExecuteSql(sql);
        }
        public int ExecSqlAndReturnID(string strSQL, string[] strParams, object[] strValues)
        {
            return db.ExecSqlAndReturnID(strSQL, strParams, strValues);
        }
        public bool ExecuteUpdate(string strSql, string[] strParams, object[] strValues)
        {
            return db.ExecuteUpdate(strSql, strParams, strValues);
        }
        #endregion
        #region 返回整个页面流


        /// <summary>
        /// 返回整个页面str
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetPage(string url, Encoding encoding)
        {
            //初始化新的request对象
            var webRequest = (HttpWebRequest)WebRequest.Create(url);
            //返回Internet资源的相应
            var webResponse = (HttpWebResponse)webRequest.GetResponse();
            //获取流
            var stream = webResponse.GetResponseStream();
            //读取编码
            var reader = new StreamReader(stream, encoding);
            //整个页面内容
            var content = reader.ReadToEnd();
            reader.Close();
            webResponse.Close();
            return content;
        }
        /// <summary>
        /// 返回整个页面str
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetHtmlCode(string url)
        {
            string htmlCode;
            var webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            webRequest.Timeout = 30000;
            webRequest.Method = "GET";
            webRequest.UserAgent = "Mozilla/4.0";
            webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            var webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
            if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
            {
                using (var streamReceive = webResponse.GetResponseStream())
                {
                    using (var zipStream =
                        new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                    {
                        using (var sr = new System.IO.StreamReader(zipStream, Encoding.UTF8))
                        {
                            htmlCode = sr.ReadToEnd();
                        }
                    }
                }
            }
            else
            {
                using (var streamReceive = webResponse.GetResponseStream())
                {
                    using (var sr = new System.IO.StreamReader(streamReceive, Encoding.UTF8))
                    {
                        htmlCode = sr.ReadToEnd();
                    }
                }
            }

            return htmlCode;
        }
        #endregion

    }
}
